package com.unicom.projt.account;

import com.unicom.projt.db.DB;
import com.unicom.projt.http.AbstractHttpController;
import com.unicom.projt.http.UriConstants;
import com.unicom.projt.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-13
 * Time: 下午1:45
 */
public class ChangePasswordController extends AbstractHttpController<String> {

    private ChangePasswordPage mActivity;
    private DB mDB;
    private String phoneNumber;
    private String newPwd;

    public ChangePasswordController(ChangePasswordPage changePasswordPage) {
        super(String.class);
        this.mActivity = changePasswordPage;
        mDB = DB.getInstance(mActivity);
    }

    public void submitNewPassword(String phoneNumber, String oldPwd, String newPws) {
        String  svcCont = "<SvcCont><TelNo>" + phoneNumber + "</TelNo><OldUserPas>" + oldPwd + "</OldUserPas>" + "<NewUserPas>" + newPws + "</NewUserPas></SvcCont>";
        String postXml = new PostXmlBuilder().setBiPCode(RequestCode.CHANGE_PASSWORD).setTelNo(phoneNumber).setSvcCont(svcCont).createXml();
        this.sendRequest(UriConstants.URL, postXml);
    }

    @Override
    public void onNetworkAbort() {
        ToastUtils.showMessage(mActivity, "错误，网络连接被中断.");
        mActivity.hideProgressDialog();
    }

    @Override
    public void onNetworkError() {
        ToastUtils.showMessage(mActivity, "错误，网络连接被中断.");
        mActivity.hideProgressDialog();
    }

    @Override
    public void onRequestFinish(String response) {
        mActivity.hideProgressDialog();
        if(response==null || "".equals(response) ){
            ToastUtils.showMessage(mActivity,"错误，请稍候再试");
            return;
        }
//        ToastUtils.showMessage(mActivity,response);
        String respCode=SimpleXmlParser.getResponseCode(response);
        if (ResponseCode.SUCCESS.equals(respCode)) {
            mDB.open();
            mDB.updatePassword(UserHolder.phoneNumber, newPwd);
            mDB.open();
            ToastUtils.showMessage(mActivity,"密码修改成功");
            mActivity.doLogout();
            UserHolder.clean();
            mActivity.finish();
        }else{
            ToastUtils.showMessage(mActivity,"修改失败,"+SimpleXmlParser.getResponseDes(response));
        }
    }

    @Override
    public void onResponseError() {
        ToastUtils.showMessage(mActivity, "错误，返回结果错误.");
        mActivity.hideProgressDialog();
    }
}
